<?php

namespace App\Http\Requests\Api;


use Illuminate\Validation\Rule;
use App\Models\GoodsCollection;
use App\Models\Goods;
class GoodsCollectionRequest extends FormRequest
{


    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        switch ($this->method()) {
            case "POST":
            {
                return [
                    "goods_id" => ["required",
                    function ($attribute, $value, $fail) {
                        if (!Goods::where(['id'=>$value,"on_sale"=>1])->first()){
                            return $fail("该商品不存在");
                        }
                        if (GoodsCollection::where(['goods_id' => $value, 'user_id' => $this->user()->id])->first()) {
                            return $fail("已添加该产品");
                        }
                    }]
                ];
            }
            case "DELETE":{
                return [
                    "goods_id"=>['required'],
                ];
            }
        }
    }
}
